package cn.wolfcode.p2p.business.service.impl;

import cn.wolfcode.p2p.base.domain.Account;
import cn.wolfcode.p2p.business.domain.AccountFlow;
import cn.wolfcode.p2p.business.domain.BidRequest;
import cn.wolfcode.p2p.business.domain.PaymentSchedule;
import cn.wolfcode.p2p.business.domain.RechargeOffline;
import cn.wolfcode.p2p.business.mapper.AccountFlowMapper;
import cn.wolfcode.p2p.business.service.IAccountFlowService;
import cn.wolfcode.p2p.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

@Service
@Transactional
public class AccountFlowServiceImpl implements IAccountFlowService {

    @Autowired
    private AccountFlowMapper accountFlowMapper;

    public AccountFlow save(BigDecimal amount, Account account, Integer actionType, String note) {
        AccountFlow accountFlow = new AccountFlow();
        accountFlow.setActionType(actionType);
        accountFlow.setAmount(amount);
        accountFlow.setNote(note);
        accountFlow.setUsableAmount(account.getUsableAmount());
        accountFlow.setFreezedAmount(account.getFreezedAmount());
        accountFlow.setActionTime(new Date());
        accountFlow.setAccountId(account.getId());
        accountFlowMapper.insert(accountFlow);
        return accountFlow;
    }

    @Override
    public void createRechargeFlow(RechargeOffline ro, Account account) {
        save(ro.getAmount(), account, Constants.ACCOUNT_ACTIONTYPE_RECHARGE_OFFLINE,
                "线下充值金额:" + ro.getAmount() + "元, 时间为:" + new Date().toLocaleString());
    }

    @Override
    public void createBidFlow(Account account, BigDecimal amount) {
        save(amount, account, Constants.ACCOUNT_ACTIONTYPE_BID_FREEZED, "投标金额:" + amount + "元, 时间为:" + new Date().toLocaleString());
    }

    @Override
    public void createBidFailureFlow(Account bidAccount, BigDecimal amount, BidRequest br) {
        save(amount, bidAccount, Constants.ACCOUNT_ACTIONTYPE_BID_UNFREEZED,
                "投标 [" + br.getTitle() + "] 失败,冻结减少,可用余额增加:" + amount + " 时间为:" + new Date().toLocaleString());
    }

    @Override
    public void createBidRequestFlow(Account account, BidRequest br) {
        save(br.getBidRequestAmount(), account, Constants.ACCOUNT_ACTIONTYPE_BIDREQUEST_SUCCESSFUL,
                br.getTitle() + "借款成功,可用余额增加:" + br.getBidRequestAmount() + " 时间为:" + new Date().toLocaleString());
    }

    @Override
    public void createPayBorrowManagementChargeFlow(BigDecimal charge, Account account, BidRequest br) {
        save(charge, account, Constants.ACCOUNT_ACTIONTYPE_CHARGE,
                br.getTitle() + "-支付平台管理费:" + charge + " 时间为:" + new Date().toLocaleString());

    }

    @Override
    public void createFreezedAmountFlow(BigDecimal amount, Account bidAccount, BidRequest br) {
        save(amount, bidAccount, Constants.ACCOUNT_ACTIONTYPE_BID_SUCCESSFUL,
                br.getTitle() + "-投标成功,冻结金额减少" + amount + " 时间为:" + new Date().toLocaleString());


    }

    @Override
    public void createReturnMoneyFlow(Account account, BigDecimal totalAmount, PaymentSchedule ps) {
        save(totalAmount, account, Constants.ACCOUNT_ACTIONTYPE_RETURN_MONEY,
                "还款成功,可用金额减少" + totalAmount + " 时间为:" + new Date().toLocaleString());

    }

    @Override
    public void createReceiveFlow(Account account, BigDecimal amount, PaymentSchedule ps) {
        save(amount, account, Constants.ACCOUNT_ACTIONTYPE_CALLBACK_MONEY,
                "收到  [" + ps.getBidRequestTitle() + " ]还款 第" + ps.getMonthIndex() + "期 ,可用金额增加:" + amount + " 时间为:" + new Date().toLocaleString());
    }

    @Override
    public void createManagerChargeFlow(Account account, BigDecimal amount) {
        save(amount, account, Constants.ACCOUNT_ACTIONTYPE_INTEREST_SHARE,
                "支付平台利息管理费,可用金额减少:" + amount + " 时间为:" + new Date().toLocaleString());
    }

    @Override
    public void createRechargeOnlineSuccessFlow(Account account, BigDecimal amount) {
        save(amount, account, Constants.ACCOUNT_ACTIONTYPE_RECHARGE_ONLINE,
                "线上托管充值,金额增加:" + amount);
    }
}
